package com.itheima.redbaby.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;
import com.itheima.redbaby.BaseActivity.MyHttpTask;
import com.itheima.redbaby.bean.BrandValue;
import com.itheima.redbaby.bean.Product;
import com.itheima.redbaby.bean.Topic;
import com.itheima.redbaby.bean.Value;
import com.itheima.redbaby.engine.BrandEngine;
import com.itheima.redbaby.engine.BrandPLlistEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.utils.ImageUtil;
/***
 *   品牌推荐
 */
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class BrandActivity extends BaseActivity {

	private TextView textTitle;
	private List<Value> brandInfo;
	private ExpandableListView listBrandInfo;
	private List<BrandValue> gridData;
	private MyExpandableListViewAdapter expandableAdapter;
	private BrandEngine impl;
	private GridView nineGv;
	private String src;
	private List<String> brandData;
	private MyGridViewAdapter gridAdapter;
	private  List<Product> geBrandPList;  
	private  TextView  backTv;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.brand_activity);
		textTitle = (TextView) findViewById(R.id.textTitle);
		textTitle.setText("推荐品牌");
		listBrandInfo = (ExpandableListView) findViewById(R.id.listBrandInfo);
		
		listBrandInfo.setGroupIndicator(null);

		
		expandableAdapter = new MyExpandableListViewAdapter();
		gridAdapter = new MyGridViewAdapter();
		brandData = new ArrayList<String>();
		gridData = new ArrayList<BrandValue>();
		impl = DaoAndEngineFactory.getImpl(BrandEngine.class);
		initdata();
		backTv = (TextView) findViewById(R.id.backTv);

		backTv.setOnClickListener( new  OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent_home = new Intent(BrandActivity.this, HomeActivity.class);
					startActivity(intent_home);
					finish();
				}
			});

	}

	private void initdata() {

		new MyHttpTask() {

			/***
			 * 获取后台数据
			 */
			@Override
			protected Object doInBackground(Void... params) {
				brandInfo = impl.getBrandInfo();

				return brandInfo;
			}

			/**
			 * 获取后台返回 的数据 并且跟新ui 获得的是 topic 对象
			 * 
			 * 更新 holder.textContent.setText("酷 ~宝贝  ~宝贝~ 裤" + "时间");
			 * holder.imgIcon.setImageResource(imgsid[position]);
			 */
			@Override
			protected void onPostExecute(Object result) {
				brandInfo = (List<Value>) result;
				for (int i = 0; i < brandInfo.size(); i++) {
					Value value = brandInfo.get(i);
					String key = value.getBrandkey();
					brandData.add(key);
					List<BrandValue> value2 = value.getValue();
					for (int j = 0; j < value2.size() - 1; j++) {
						BrandValue brandValue = value2.get(j);
						src  = brandValue.getSrc();
						System.out.println(brandValue.toString());
						gridData.add(brandValue);
					}

				}
				listBrandInfo.setAdapter(expandableAdapter);

				expandableAdapter.notifyDataSetChanged();

			}

		}.executeHttp();
		/**
		 * listview 中的跳转事件 点击时候我们 跳转到商品列表页面
		 */
		// nineGv.setOnItemClickListener(new OnItemClickListener() {

		// @Override
		// public void onItemClick(AdapterView<?> parent, View view,
		// int position, long id) {
		// Toast.makeText(getApplicationContext(), "你点击的的是"+position, 1).show();
		// Topic topic = cuxiaoListData.get(position);
		// String topicId = topic.getId();
		// Intent intent = new Intent(PromBulletinActivity.this,
		// CuXiaoProductListActivity.class);
		// intent.putExtra("topicId", topicId);
		// startActivity(intent);
		// }
		// });
		//

	}

	class MyExpandableListViewAdapter extends BaseExpandableListAdapter {

		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return brandData.size() - 1;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generated method stub
			return 1;
		}

		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			TextView tv = new TextView(getApplicationContext());
			listBrandInfo.expandGroup(groupPosition);
			tv.setText(brandData.get(groupPosition));
			tv.setTextColor(Color.RED);
			return tv;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			View view = View.inflate(BrandActivity.this,
					R.layout.brand_child_list, null);
			nineGv = (GridView) view.findViewById(R.id.nineGv);
			nineGv.setOnItemClickListener(new OnItemClickListener() {
			
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
				
				Intent intent = new Intent(BrandActivity.this,
						CuXiaoProductListActivity.class);
				intent.putExtra("topicId", "u100");
				startActivity(intent);
					}
			});
			nineGv.setAdapter(gridAdapter);
			gridAdapter.notifyDataSetChanged();
			return view;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return false;
		}

	}
	/***
	 * 图片适配器
	 * 
	 * @author xun
	 * 
	 */

	class MyGridViewAdapter extends BaseAdapter {

		@Override
		public int getCount() {

			return gridData.size() - 1;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View view = View.inflate(BrandActivity.this,
					R.layout.brand_child_item, null);
			ImageView brandIconIv = (ImageView) view
					.findViewById(R.id.brandIconIv);
			BrandValue brandValue = gridData.get(position);
			String  uri =  brandValue.getSrc();
			ImageUtil.loadImageFromUrl(brandIconIv,uri);
		
			return view;
		}

	}

}
